package com.xmkj.bearuser.member;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.constumpop.CustomPopWindow;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/6/14
 * 描述：
 */

public class SearchAndMemberListActivity extends BaseMvpActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_menber_title)
    TextView tvMenberTitle;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.rl_search)
    RelativeLayout rlSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_data_sort)
    CheckedTextView tvDataSort;
    @BindView(R.id.tv_filter)
    CheckedTextView tvFilter;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @BindView(R.id.drawer_content)
    RelativeLayout drawerContent;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    List<String> bean = new ArrayList<>();
    private MemberListAdapter adapter;
    private CustomPopWindow mCustomPopWindow;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_memberlist;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(tvFilter);
        bean.add("");
        bean.add("");
        bean.add("");
        gotoHttpRep();
        setRecyclerView();
        initDrawbleLisner();
        attachClickListener(tvDataSort);
    }

    private void initDrawbleLisner() {
        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            /**
             * 当抽屉滑动状态改变的时候被调用
             * 状态值是STATE_IDLE（闲置--0）, STATE_DRAGGING（拖拽的--1）, STATE_SETTLING（固定--2）中之一。
             * 抽屉打开的时候，点击抽屉，drawer的状态就会变成STATE_DRAGGING，然后变成STATE_IDLE
             */
            @Override
            public void onDrawerStateChanged(int arg0) {
                Log.i("drawer", "drawer的状态：" + arg0);
            }

            /**
             * 当抽屉被滑动的时候调用此方法
             * arg1 表示 滑动的幅度（0-1）
             */
            @Override
            public void onDrawerSlide(View arg0, float arg1) {
                Log.i("drawer", arg1 + "");
            }

            /**
             * 当一个抽屉被完全打开的时候被调用
             */
            @Override
            public void onDrawerOpened(View arg0) {
                Log.i("drawer", "抽屉被完全打开了！");
                tvFilter.setChecked(true);
                tvDataSort.setChecked(false);
            }

            /**
             * 当一个抽屉完全关闭的时候调用此方法
             */
            @Override
            public void onDrawerClosed(View arg0) {
                Log.i("drawer", "抽屉被完全关闭了！");
                tvFilter.setChecked(false);

            }
        });
    }

    private void setRecyclerView() {
//        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
       /* GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);*/
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setLoadMoreEnabled(true);
        adapter = new MemberListAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }


    private void gotoHttpRep() {
    /*    CommonSubscriber<ZhongJiangEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ZhongJiangEntity beans = (ZhongJiangEntity) o;

                List<ZhongJiangEntity.ListsBean> beanList = beans.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        ZhongJiangListMethods.getInstance().openList(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), mPageIndex);
        rxManager.add(subscriber);*/
    }
private  boolean isDataSortChecked = false ;
    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvFilter.getId()) {
            drawerLayout.openDrawer(drawerContent);
        } else if(view.getId()==tvDataSort.getId()){
            if(isDataSortChecked){  // 已经被选中了  升序状态
                tvDataSort.setChecked(false);
                tvFilter.setChecked(false);
                isDataSortChecked =false;

                // 处理pop
            }else {
                tvFilter.setChecked(false);
                tvDataSort.setChecked(true);
                isDataSortChecked=true;
                //      弹出pop
                showPop();
            }
        }
    }

    private void showPop() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_menu,null);
        //处理popWindow 显示内容
        handleLogic(contentView);
        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
//                .enableOutsideTouchableDissmiss(false)
                .size(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create()
                .showAsDropDown(tvDataSort,0,10);
    }

    private void handleLogic(View contentView) {

        RadioGroup rg =     contentView.findViewById(R.id.radio_group);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mCustomPopWindow.dissmiss();
                tvDataSort.setChecked(true);
                tvFilter.setChecked(false);
                isDataSortChecked = false;

                switch (checkedId) {
                    case R.id.rb_jifen:
                        tvDataSort.setText("积分");
                        break;
                    case R.id.rb_register_time:
                        tvDataSort.setText("注册时间");
                        break;
                    case R.id.rb_kaika_date:
                        tvDataSort.setText("开卡日期");
                        break;
                    case R.id.rb_comstum_date:
                        tvDataSort.setText("消费日期");
                        break;
                    case R.id.rb_huifang_date:
                        tvDataSort.setText("回访日期");
                        break;
                    case R.id.rb_comstum_time:
                        tvDataSort.setText("消费次数");
                        break;
                    case R.id.rb_all_money:
                        tvDataSort.setText("总消费额");
                        break;

                }
            }
        });
        /*
        contentView.findViewById(R.id.rb_register_time).setOnClickListener(listener);
        contentView.findViewById(R.id.rb_kaika_date).setOnClickListener(listener);
        contentView.findViewById(R.id.rb_comstum_date).setOnClickListener(listener);
        contentView.findViewById(R.id.rb_huifang_date).setOnClickListener(listener);
        contentView.findViewById(R.id.rb_comstum_time).setOnClickListener(listener);
        contentView.findViewById(R.id.rb_all_money).setOnClickListener(listener);*/
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }


}
